<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()" :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
          <el-form-item prop="userId" :label="$t('order.userId')">
          <el-input v-model="dataForm.userId" :placeholder="$t('order.userId')" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item prop="userName" :label="$t('order.userName')">
          <el-input v-model="dataForm.userName" :placeholder="$t('order.userName')" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item prop="commodityName" :label="$t('order.commodityName')">
          <el-input v-model="dataForm.commodityName" :placeholder="$t('order.commodityName')" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
      <el-form-item prop="commodityType" :label="$t('order.commodityType')" size="mini">
        <el-radio-group v-model="dataForm.commodityType" :disabled="!!dataForm.id">
          <el-radio :label="1">{{ $t('order.commodityType1') }}</el-radio>
          <el-radio :label="2">{{ $t('order.commodityType2') }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="payType" :label="$t('order.payType')" size="mini">
        <el-radio-group v-model="dataForm.payType" :disabled="!!dataForm.id">
          <el-radio :label="1">{{ $t('order.payType1') }}</el-radio>
          <el-radio :label="2">{{ $t('order.payType2') }}</el-radio>
          <el-radio :label="3">{{ $t('order.payType3') }}</el-radio>
          <el-radio :label="4">{{ $t('order.payType4') }}</el-radio>
          <el-radio :label="5">{{ $t('order.payType5') }}</el-radio>
        </el-radio-group>
      </el-form-item>
          <el-form-item prop="orderCode" :label="$t('order.orderCode')">
          <el-input v-model="dataForm.orderCode" :placeholder="$t('order.orderCode')" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item prop="trilateralCode" :label="$t('order.trilateralCode')">
          <el-input v-model="dataForm.trilateralCode" :placeholder="$t('order.trilateralCode')" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item prop="rechargeAmount" :label="$t('order.rechargeAmount')">
          <el-input v-model="dataForm.rechargeAmount" :placeholder="$t('order.rechargeAmount')" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
      <el-form-item prop="orderType" :label="$t('order.orderType')" size="mini">
        <el-radio-group v-model="dataForm.orderType" :disabled="!!dataForm.id">
          <el-radio :label="0">{{ $t('order.orderType0') }}</el-radio>
          <el-radio :label="1">{{ $t('order.orderType1') }}</el-radio>
          <el-radio :label="2">{{ $t('order.orderType2') }}</el-radio>
          <el-radio :label="3">{{ $t('order.orderType3') }}</el-radio>
          <el-radio :label="4">{{ $t('order.orderType4') }}</el-radio>
        </el-radio-group>
      </el-form-item>
          <el-form-item label="创建时间" prop="foundTime">
          <el-input v-model="dataForm.foundTime" placeholder="创建时间" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item label="操作时间" prop="operateTime">
          <el-input v-model="dataForm.operateTime" placeholder="操作时间" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item label="超时时间" prop="overTime">
          <el-input v-model="dataForm.overTime" placeholder="超时时间" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
          <el-form-item label="返佣金额" prop="commission">
          <el-input v-model="dataForm.commission" placeholder="返佣金额" :disabled="!!dataForm.id"></el-input>
      </el-form-item>
      </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: '',
        userId: '',
        userName: '',
        commodityId: '',
        commodityName: '',
        commodityType: '',
        payId: '',
        payType: '',
        orderCode: '',
        trilateralCode: '',
        rechargeAmount: '',
        orderType: '',
        foundTime: '',
        operateTime: '',
        overTime: '',
        commission: ''
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/sys/order/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {})
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/sys/order/', this.dataForm).then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {})
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
